package com.nightpiter.view.drawer.dynamic;

import java.util.ArrayList;
import java.util.HashMap;

import com.nightpiter.controller.log.Logger;
import com.nightpiter.model.MetroModelData;
import com.nightpiter.model.objects.Hub;
import com.nightpiter.model.objects.Station;
import com.nightpiter.model.objects.Transfer;
import com.nightpiter.model.objects.Transition;
import com.nightpiter.model.objects.Tunnel;
import com.nightpiter.view.Calculator.Mode;

/**
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
class Structure {

	private HashMap<Station, PrimitiveAmoeba> primitiveAmoebasMap;

	private HashMap<Transition, PrimitiveBacillus> primitiveBacillusMap = new HashMap<Transition, PrimitiveBacillus>();

	private AmoebaUltimate[] amoebas;

	private ArrayList<BacillusUltimate> bacilli = new ArrayList<BacillusUltimate>();

	public Structure(MetroModelData modelData, Mode mode) {

		primitiveAmoebasMap = new HashMap<Station, PrimitiveAmoeba>(modelData
				.getStations().size(), 1);
		ArrayList<AmoebaUltimate> amoebasList = new ArrayList<AmoebaUltimate>();

		for (Hub hub : modelData.getHubs()) {
			AmoebaUltimate amoebaUltimate = new AmoebaUltimate(hub, mode, this);
			amoebasList.add(amoebaUltimate);
			primitiveAmoebasMap.putAll(amoebaUltimate.getPrimitiveAmoebasMap());
		}

		for (Station station : modelData.getStations()) {
			if (station.getHub() == null) {
				AmoebaUltimate amoebaUltimate = new AmoebaUltimate(station,
						mode, this);
				amoebasList.add(amoebaUltimate);
				primitiveAmoebasMap.putAll(amoebaUltimate
						.getPrimitiveAmoebasMap());
			}
		}

		amoebas = (AmoebaUltimate[]) amoebasList
				.toArray(new AmoebaUltimate[amoebasList.size()]);
		Logger.log("amoebas count: " + amoebas.length);

		for (Tunnel tunnel : modelData.getTunnels()) {
			PrimitiveAmoeba fromAmoeba = primitiveAmoebasMap.get(tunnel
					.getFromStation());
			PrimitiveAmoeba toAmoeba = primitiveAmoebasMap.get(tunnel
					.getToStation());
			PrimitiveBacillus bacillus = fromAmoeba
					.searchPrimitiveBacillus(toAmoeba);
			bacillus.addTunnel(fromAmoeba, toAmoeba, tunnel);
			primitiveBacillusMap.put(tunnel, bacillus);
		}

		for (Transfer transfer : modelData.getTransfers()) {
			PrimitiveAmoeba fromAmoeba = primitiveAmoebasMap.get(transfer
					.getFromStation());
			PrimitiveAmoeba toAmoeba = primitiveAmoebasMap.get(transfer
					.getToStation());
			PrimitiveBacillus bacillus = fromAmoeba
					.searchPrimitiveBacillus(toAmoeba);
			bacillus.addTransfer(fromAmoeba, toAmoeba, transfer);
			primitiveBacillusMap.put(transfer, bacillus);
		}

		Logger.log("bacilli count: " + bacilli.size());
		for (BacillusUltimate bacillusUltimate : bacilli) {
			bacillusUltimate.build();
		}

	}

	public void addBacillusUltimate(BacillusUltimate bacillusUltimate) {
		bacilli.add(bacillusUltimate);
	}

	public HashMap<Station, PrimitiveAmoeba> getPrimitiveAmoebasMap() {
		return primitiveAmoebasMap;
	}
	
	public HashMap<Transition, PrimitiveBacillus> getPrimitiveBacillusMap() {
		return primitiveBacillusMap;
	}
	
	public ArrayList<Amoeba> getDrawnAmoebas() {
		ArrayList<Amoeba> drawnAmoebas = new ArrayList<Amoeba>();
		for (AmoebaUltimate amoeba : amoebas) {
			amoeba.addDrawnAmoebas(drawnAmoebas);
		}
		return drawnAmoebas;
	}

	public AmoebaUltimate[] getAmoebas() {
		return amoebas;
	}

	public ArrayList<BacillusUltimate> getBacilli() {
		return bacilli;
	}

}
